<?php

/**
* main class
* 
* @author cooper
*/
class Index extends Controller
{
	/**
	 *	Construct
	 */
	public function __construct()
	{
		parent::__construct();
	}
	
    
    /**
    * default action
    * 
    */
    public function index()
    {
        
        // get all portfolio works
        $portfolio = $this->model->getAllPortfolioWorks();
        // set view varible
        $this->view->portfolio = $portfolio;
        
        // render view
        $this->view->render('index/index');
    }
	
    
    /***********************************************
    *       ADMIN SECTION
    *///////////////////////////////////////////////
    
    
    public function admin_index()
    {
        
        $this->checkLoggin();
        
        // get all index works
        $res = $this->model->getAllPortfolioWorks();
        // set view varible
        $this->view->index = $res;
        // render view
        $this->view->render('index/admin_index');
        exit();
    }
    
    
    
    public function admin_add()
    {
        
        $this->checkLoggin();
        
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            
            $data = $_POST;
            if(isset($_FILES['foto_work']) && $_FILES['foto_work']['error'] == 0 && strlen($_FILES['foto_work']['tmp_name']) > 0)
            {
                // include upload lib
                require 'lib/file_image_upload.php';
                $file = new FileImageUpload();
                
                $res =  $file->upload_process($_FILES['foto_work'], UPLOAD_PORTFOLIO_IMAGE_DIR);
                
                if($res && strlen($res) > 4) 
                {
                    $data['foto_work'] = $res;   
                } else {
                    $data['foto_work'] = '';
                }
            }
            
            
            $this->model->admin_add_save($data); 
            exit();  
        }
        
        $this->view->render('index/admin_add');
        exit();
    } 
    
    /**
    * edit index works
    * 
    */
    public function admin_edit()
    {
        
        $this->checkLoggin();
        
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            
            $data = $_POST;
            if(isset($_FILES['foto_work']) && $_FILES['foto_work']['error'] == 0 && strlen($_FILES['foto_work']['tmp_name']) > 0)
            {
                // include upload lib
                require 'lib/file_image_upload.php';
                $file = new FileImageUpload();
                
                $res =  $file->upload_process($_FILES['foto_work'], UPLOAD_PORTFOLIO_IMAGE_DIR);
                
                if($res && strlen($res) > 4) 
                {
                    $data['foto_work'] = $res;   
                } else {
                    $data['foto_work'] = '';
                }
            }
            
            
            $this->model->admin_add_save($data); 
            exit();  
        }
        
        $this->view->render('index/admin_add');
        exit();
    }
    
}